﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_DivineConstitution : Buff
    {
        private int HPAdded = 100;

        public Spell_DivineConstitution()
        {
            minutesLeft = 10;
        }
        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            foreach (Buff buff in TargetCreatureBase.buffManager.Buffs)
            {
                if (buff.spellIcon.Name == base.spellIcon.Name && buff.SpellID != SpellID)
                {
                    buff.RemoveSpell(false, false);
                }
            }

            HPAdded = SkillManager.AdjustForCombat(castLevel, 0, castWis) * 2;

            if (spellIcon.Skill == "Holy")
                TargetCreatureBase.MaxHPAdd = TargetCreatureBase.MaxHPAdd + HPAdded;
            else if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.MaxManaAdd = TargetCreatureBase.MaxManaAdd + HPAdded;
            else if (spellIcon.Skill == "Might")
                TargetCreatureBase.MaxStaminaAdd = TargetCreatureBase.MaxStaminaAdd + HPAdded;

            TargetCreatureBase.Update_Character();

            spellTimer.Start();
        }
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            minutesLeft = System.Convert.ToInt32(Math.Floor(CastingCreatureWis / 2.00));

            HPAdded = SkillManager.AdjustForCombat(CastLevel, 0, CastingCreatureWis) * 2;

            if (spellIcon.Skill == "Holy")
                TargetCreatureBase.MaxHPAdd = TargetCreatureBase.MaxHPAdd + HPAdded;
            else if (spellIcon.Skill == "Mentality")
                TargetCreatureBase.MaxManaAdd = TargetCreatureBase.MaxManaAdd + HPAdded;
            else if (spellIcon.Skill == "Might")
                TargetCreatureBase.MaxStaminaAdd = TargetCreatureBase.MaxStaminaAdd + HPAdded;

            TargetCreatureBase.Update_Character();
            TargetCreatureBase.buffManager.UpdateBuffs();
 
            //We are changing creatures HP so we need to send out the creature base update
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;

                            if (spellIcon.Skill == "Holy")
                                character.UpdateCreatureBaseHp(TargetCreatureBase);
                            else if (spellIcon.Skill == "Mentality")
                                character.UpdateCreatureBaseMana(TargetCreatureBase);
                            else if (spellIcon.Skill == "Might")
                                character.UpdateCreatureBaseStamina(TargetCreatureBase);
                        }
                    }
                }
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;
                            character.PlaySound("BuffCast", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                        }
                    }
                }
            }
           

            //Start Spell Timer
            spellTimer.Start();
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);

            LockItem lockItem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
            lock (lockItem.LockObj)
            {
                Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);

                if (spellIcon.Skill == "Holy")
                    TargetCreatureBase.MaxHPAdd = TargetCreatureBase.MaxHPAdd - HPAdded;
                else if (spellIcon.Skill == "Mentality")
                    TargetCreatureBase.MaxManaAdd = TargetCreatureBase.MaxManaAdd - HPAdded;
                else if (spellIcon.Skill == "Might")
                    TargetCreatureBase.MaxStaminaAdd = TargetCreatureBase.MaxStaminaAdd - HPAdded;

                if (TargetCreatureBase.HP > TargetCreatureBase.MaxHP)
                    TargetCreatureBase.HP = TargetCreatureBase.MaxHP;
                if (TargetCreatureBase.Mana > TargetCreatureBase.MaxMana)
                    TargetCreatureBase.Mana = TargetCreatureBase.MaxMana;
                if (TargetCreatureBase.Stamina > TargetCreatureBase.MaxStamina)
                    TargetCreatureBase.Stamina = TargetCreatureBase.MaxStamina;

                TargetCreatureBase.Update_Character();

                List<Character> CharsToUpdate = new List<Character>();
                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                                CharsToUpdate.Add((Character)creaturebase);
                        }
                    }
                }
                foreach (Character character in CharsToUpdate)
                {
                    if (spellIcon.Skill == "Holy")
                        character.UpdateCreatureBaseHp(TargetCreatureBase);
                    else if (spellIcon.Skill == "Mentality")
                        character.UpdateCreatureBaseMana(TargetCreatureBase);
                    else if (spellIcon.Skill == "Might")
                        character.UpdateCreatureBaseStamina(TargetCreatureBase);
                }
            }
        }
    }
}
